#include "Block.h"
#include "Configuration.h"
#include "UpdateBlockNetworkEvent.h"
#include "FadeOutAnimation.h"

Block::Block(const Position& position, const Color& color)
{
    init(color, position);
}

Block::Block(long id, const Position& position, const Color& color)
        : GraphicalElement(id)
{
    init(color, position);
}

void Block::runFadeOutAnimation(long rowId)
{
    Animation* animation = new FadeOutAnimation(300);
    animation->setDoneDestinationId(rowId);
    setAnimation(animation);
}

void Block::init(const Color& color, const Position& position)
{
    Size size = Size(1, 1).scale(Configuration::BLOCK_SIZE);
    setTexture(GraphicsWrapper::loadBlockCached(size, color));
    setPosition(position.scale(Configuration::BLOCK_SIZE));
}

void Block::handleUpdateBlockEvent(long blockId, const Position& newPosition)
{
    if (blockId == getId())
    {
        setPosition(newPosition.scale(Configuration::BLOCK_SIZE));
    }
}

void Block::handleEvent(const Event& event)
{
    event.visitBlock(this);
}

Block::~Block()
{
}
